package com.example.algorithm.LinkedListAlgorithm;

/**
 * 单向链表
 * @author ZMJ
 * @date 2020/3/23
 */
public class Node {
    private String key;
    private String value;
    private Node next;
    public Node(String key, String value) {
        this.key = key;
        this.value = value;
    }
    public void setNext(Node next) {
        this.next = next;
    }
    public Node getNext() {
        return this.next;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String toString() {
        return  this.key + ":" + this.value;
    }

    // 打印一个链表
    public static void printLinkedList(Node head) {
        while (head != null) {
            if (head.getNext() == null) {
                System.out.print(head.toString());
            } else {
                System.out.print(head.toString() + ">");
            }
            head = head.getNext();
        }
        System.out.println("");
    }

    // 构建一个长度为length的有序链表
    public static Node createLinkedList(int length) {
        Node node = new Node("0", "0");
        Node head = node;
        for (int i = 1; i < length; i++) {
            node.setNext(new Node(i + "", i + ""));
            node = node.getNext();
        }
        return head;
    }

    // 根据数组构建以个单向链表
    public static Node createLinkedListByArr(String[] arr) {
        Node head = new Node("-1", "-1");
        Node end = head;
        for (String tem : arr) {
            end.setNext(new Node(tem, tem));
            end = end.getNext();
        }
        return head.getNext();
    }

}
